package com.qf.business.course.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ken.event.action.apply.producer.EventTemplate;
import com.qf.business.course.core.dao.CourseInfoDao;
import com.qf.business.course.core.service.CourseInfoGuigeService;
import com.qf.business.course.core.service.CourseInfoService;
import com.qf.commons.core.utils.CommonsUtils;
import com.qf.commons.core.utils.QfBeanUtils;
import com.qf.commons.web.aspect.annotation.GetUser;
import com.qf.commons.web.utils.UserUtils;
import com.qf.data.course.dto.CourseSearchDto;
import com.qf.data.course.entity.CourseInfo;
import com.qf.data.course.entity.CourseInfoGuige;
import com.qf.data.course.vo.input.CourseInfoInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程信息表(CourseInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-09-23 09:25:37
 */
@Service("courseInfoService")
@Slf4j
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoDao, CourseInfo> implements CourseInfoService {

    @Autowired
    private CourseInfoGuigeService courseInfoGuigeService;

    /**
     * 根据教师id 查询课程列表
     * @param tid
     * @return
     */
    @Override
    public List<CourseInfo> queryByTid(Integer tid) {
        List<CourseInfo> courseList = this.query().eq("teacher_id", tid).list();
        return courseList;
    }

    /**
     * 根据指定的分类id 查询当前分类下的所有课程信息
     * @param tid
     * @return
     */
    @Override
    public List<CourseInfo> queryByOneType(Integer tid) {
        //11 -> 0011
        String idStr = CommonsUtils.id2Str(tid, 4);
        //查询
        return getBaseMapper().queryCourseByTid(idStr);
    }

    /**
     * 保存课程信息
     * @param courseInfoInput
     * @return
     */
    @Override
    @GetUser
    @Transactional
    public int save(CourseInfoInput courseInfoInput) {
        //保存基本课程
        CourseInfo courseInfo = new CourseInfo();
        BeanUtils.copyProperties(courseInfoInput, courseInfo);
        //设置课程申请的教师id
        courseInfo.setTeacherId(UserUtils.getUser().getId());
        log.debug("[save course] 保存的课程信息 - {}", courseInfo);
        //保存课程信息
        this.save(courseInfo);

        //将对象做一个转换
        CourseSearchDto courseSearchDto = QfBeanUtils.copyBean(courseInfo, CourseSearchDto.class);

        //-------处理课程和规格的关联关系---------
        //[{"gid":1,"selected":1},{"gid":4,"selected":15}]
        List<CourseInfoGuige> courseInfoGuiges = new ArrayList<>();
        List<CourseSearchDto.Guiges> guiges = new ArrayList<>();
        courseSearchDto.setGuiges(guiges);

        JSONArray jsonArray = JSON.parseArray(courseInfoInput.getGuiges());
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject obj = jsonArray.getJSONObject(i);
            Integer gid = obj.getInteger("gid"); //规格id
            Integer selected = obj.getInteger("selected"); //规格选中的id

            //创建需要传输的DTO对象的规格列表
            guiges.add(new CourseSearchDto.Guiges().setGid(gid).setValid(selected));

            CourseInfoGuige courseInfoGuige = new CourseInfoGuige()
                    .setCid(courseInfo.getId())//课程id
                    .setCgid(gid)//规格id
                    .setCgvid(selected);//规格值的id
            courseInfoGuiges.add(courseInfoGuige);
        }
        courseInfoGuigeService.saveBatch(courseInfoGuiges);

        //发送事件总线，告知搜索服务 保存课程信息到ES
        EventTemplate.sendQuickly("course_insert", courseSearchDto);

        return 1;
    }
}